package manage.p.service;

import manage.p.bean.Customer;

/**
 * @ClassName CustomerList
 * @Description 提供管理数据, 增删改查
 * @Author wuhn Email:1845979768@qq.com
 * @Date 2021/8/11 21:32
 * @Version 1.0
 */
public class CustomerList {
    //保持客户对象数组
    private Customer[] customers;
    //记录已保持用户数量
    private int total = 0;

    /**
     * @param totalCustomer :指定数组的长度
     * @return
     * @Description 用来初始化customers数组的构造器
     * @Date 22:12 2021/8/11
     */
    public CustomerList(int totalCustomer) {
        customers = new Customer[totalCustomer];
    }

    /**
     * @param customer
     * @return true:添加成功 false:添加失败
     * @Description 将指定的用户添加到数组中
     */
    public boolean addCustomer(Customer customer) {
        if (total >= customers.length) {
            return false;
        }
        customers[total] = customer;
        total++;
        return true;
    }

    /**
     * 修改指定索引位置的客户信息
     *
     * @param index
     * @param cust
     * @return true:修改成功 false:修改失败
     */

    public boolean replaceCustomer(int index, Customer cust) {
        if (index < 0 || index >= total) {
            return false;
        }
        customers[index] = cust;
        return true;
    }

    /**
     * 删除指定索引位置用户信息
     *
     * @param index
     * @return true:删除成功 false:删除失败
     */
    public boolean deleteCustomer(int index) {
        if (index < 0 || index >= customers.length) {
            return false;
        }
        for (int i = index; i < total - 1; i++) {
            customers[i] = customers[i + 1];
        }
        //最后有数据的元素要置空
        customers[total - 1] = null;
        //数据删除一个有效数据总数也删除
        total--;
        return true;
    }

    /**
     * 获取所有客户信息
     *
     * @return
     */
    public Customer[] getAllCustomer() {
        Customer[] custs = new Customer[total];
        for (int i = 0; i < total; i++) {
            custs[i] = customers[i];
        }
        return custs;
    }

    /**
     * 获取指定索引位置客户信息
     *
     * @param index
     * @return null没有找到元素;找到元素,则返回
     */
    public Customer getCustomers(int index) {
        if (index < 0 || index >= total) {
            return null;
        }
        return customers[index];
    }

    /**获取存取用户数量
     * @return
     */
    public int getTotal() {
        return total;
    }
}
